package com.zhou.project.components.validation;

import org.springframework.util.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @project spring-boot-project
 * @author zhouhonggang
 * @datetime 2021/8/14 16:33
 * @version 1.0.0
 * @description: 自定义校验规则实现
 */
public class PasswordValidator implements ConstraintValidator<Password, String> {

    //数字
    public static final String NUMBER = ".*\\d+.*";
    //大写字母
    public static final String UPPERCASE = ".*[A-Z]+.*";
    //小写字母
    public static final String LOWERCASE = ".*[a-z]+.*";
    //特殊符号
    public static final String SYMBOL = ".*[~!@#$%^&*()_+|<>,.?/:;'\\[\\]{}\"]+.*";

    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        if(StringUtils.hasText(value))
        {
            int count = 0;
            if (value.matches(PasswordValidator.NUMBER)) count++;
            if (value.matches(PasswordValidator.LOWERCASE)) count++;
            if (value.matches(PasswordValidator.UPPERCASE)) count++;
            if (value.matches(PasswordValidator.SYMBOL)) count++;
            return count > 2;
        }
        return false;
    }

}
